package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.po.Role;
import com.neuedu.yyzx.service.RoleService;
import com.neuedu.yyzx.vo.RoleEditVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 金山
 * 项目：yyzx
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/2514:23
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 分页查询
     *
     * @param page
     * @param role
     * @return
     */
    @GetMapping("/page")
    public Ret<List<Role>> page(Page<Role> page, Role role) {

        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        if (role.getRoleName() != null) {
            queryWrapper.like(Role::getRoleName, role.getRoleName());   //  where  role_name like  ?
        }

        // 排序 ORDER BY createtime DESC
        queryWrapper.orderByDesc(Role::getCreateTime);

        // 分页查询
        page = roleService.page(page, queryWrapper);
        return Ret.ok(page);
    }


    /**
     * 用户的添加或者更细
     *
     * @param role
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Role role) {
        boolean success = roleService.saveOrUpdate(role);
        return Ret.ok(success);
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Ret<Role> get(@PathVariable int id) {
        Role user = roleService.getById(id);
        return Ret.ok(user);
    }


    /**
     * 初始化 编辑页面（添加、修改）
     *
     * @param id
     * @return
     */
    @GetMapping("/initEdit")
    public Ret<RoleEditVO> editInit(@RequestParam(required = false) Long id) {
        RoleEditVO vo = roleService.editInit(id);
        return Ret.ok(vo);
    }


    /**
     * 根据主键删除
     * <p>
     * /user/id
     * method delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(roleService.removeById(id));
    }




}
